package mylib

// 基础包
import (
	"github.com/henrylee2cn/pholcus/app/downloader/request" //必需
	"github.com/henrylee2cn/pholcus/common/goquery"         //DOM解析
	// "github.com/henrylee2cn/pholcus/logs"               //信息输出
	. "github.com/henrylee2cn/pholcus/app/spider" //必需
	// . "github.com/henrylee2cn/pholcus/app/spider/common"          //选用
	// net包
	// "net/http" //设置http.Header
	// "net/url"
	// 编码包
	// "encoding/xml"
	// "encoding/json"
	// 字符串处理包
	// "regexp"
	//"strconv"
	//"strings"
	// 其他包
	// "fmt"
	// "math"
	// "time"
	//"log"
)

func init() {
	LoroPiana.Register()
}

type news struct {
	module string //模块
	title  string //标题
	href   string //连接
	top    string
}

var LoroPiana = &Spider{
	Name:        "腾讯新闻",
	Description: "腾讯新闻分类资源抓取",
	// Pausetime: 300,
	// Keyin:   KEYIN,
	// Limit:        LIMIT,
	EnableCookie: false,

	RuleTree: &RuleTree{
		Root: func(ctx *Context) {

			ctx.AddQueue(&request.Request{
				Method: "GET",
				Url:    "http://news.qq.com/articleList/ranking/",
				Rule:   "分类列表",
			})
		},

		Trunk: map[string]*Rule{

			"分类列表": {
				ItemFields: []string{
					"模块",
					"标题",
					"链接",
					"热度",
				},
				ParseFunc: func(ctx *Context) {

					var qqnew news
					var ok bool

					//fmt.Println("分类列表")

					dom := ctx.GetDom()

					module := dom.Find(".detail .module")
					//fmt.Println(module)
					module.Each(func(i int, s *goquery.Selection) {
						qqnew.module = s.Find(".hd").Text() //分类名称

						zxhotlist := s.Find("ul li")
						//fmt.Println(zxhotlist)
						zxhotlist.Each(func(i int, s2 *goquery.Selection) {
							qqnew.top = s2.Find(".top").Text()
							qqnew.title = s2.Find(".info").Find("p a").Text()
							qqnew.href, ok = s2.Find(".info").Find("p a").Attr("href")
							//fmt.Println(qqnew, ok)
							if ok {
								ctx.Output(map[int]interface{}{
									0: qqnew.module,
									1: qqnew.title,
									2: qqnew.href,
									3: qqnew.top,
								})
							}

						})

					})
				},
			},
		},
	},
}
